package com.hkd.Dao模式.Dao.DaoImp;

import com.hkd.Dao模式.Dao.SignonDao;
import com.hkd.Dao模式.util.DataBase;
import com.hkd.Dao模式.实体类.Signon;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：陈奕安（3048279304@qq.com）
 * @date ：Created in 2025/5/27 23:01
 * @description：
 * @modified By：
 * @version:
 */
public class SignonDaoImp extends DataBase implements SignonDao {
    @Override
    public int add(Signon entity) {
        String sql="insert into sigon values(?,?)";

        return this.executeUpdate(sql,entity.getUsername(),entity.getPassword());
    }

    @Override
    public int delete(String username) {
        String sql="delete from sigon where username=?";
        return this.executeUpdate(sql,username);
    }

    @Override
    public int update(Signon entity) {
        String sql="update sigon set password=? where username=?";

        return this.executeUpdate(sql,entity.getPassword(),entity.getUsername());
    }

    @Override
    public List<Signon> findAll() {
        String sql="select * from signon";
        this.rs=this.executQuery(sql);
        List<Signon> list=new ArrayList<Signon>();

            try {  while(rs.next()){
                Signon entity=new Signon();
                entity.setUsername(rs.getString("username"));
                entity.setPassword(rs.getString("password"));
                list.add(entity);
            } }catch (SQLException e) {
                e.printStackTrace();
            }



        return list;
    }

    @Override
    public boolean login(Signon entity) {
        String sql="select * from signon where username=? and password=?";
        this.rs=this.executQuery(sql,entity.getUsername(),entity.getUsername());
        boolean tag=false;
        try{
            tag=rs.next();
        }catch (SQLException e){
            e.printStackTrace();
        }
        return tag;
    }

    @Override
    public String login1(Signon entity) {
        String msg="";
        String sql="select password from signon where username=?";
        this.rs=this.executQuery(sql,entity.getPassword());
        try{
            if (rs.next()){
                if (entity.getPassword().equals(rs.getString("password"))){
                    msg="登陆成功";
                }
                else {
                    msg="登陆失败";
                }

            }  else {
                msg="用户不存在";
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return msg;
    }
}